/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 *
 */

import { expect, test } from '@jest/globals';
import fs from 'fs';
import fse from 'fs-extra';
import path from 'path';

import { initByStageEmptyApp, initExecuteCommand, runHvigor } from './common/common';
import { commonConfig } from './plugins/common-config';

describe('test hvigor-config.json5 startParameters', () => {
  test.concurrent('hvigor-config.json5 x cli ×', async () => {
    await initByStageEmptyApp({
      cb: async (dir) => {
        const appHvigorfile = path.resolve(dir, 'hvigorfile.ts');
        fs.writeFileSync(appHvigorfile, 'import {startParameters} from \'@ohos/hvigor\';\n' +
          'console.log(startParameters);\n' +
          'export { appTasks } from \'@ohos/hvigor-ohos-plugin\';');
        const commands = initExecuteCommand({
          mode: 'module',
          product: 'default',
          module: 'entry@default',
          tasks: 'default@PreBuild',
          log: 'i'
        });
        const result = runHvigor(dir, commands, { stderr: 'pipe' });
        expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
        expect(result.stdout.includes('hvigorfileTypeCheck: false')).toBeTruthy();
        expect(result.stdout.includes('parallelExecution: true')).toBeTruthy();
        expect(result.stdout.includes('incrementalExecution: true')).toBeTruthy();
        expect(result.stdout.includes('printStackTrace: false')).toBeTruthy();
        expect(result.stdout.includes('daemon: false')).toBeTruthy();
        expect(result.stdout.includes('levelStr: \'INFO\'')).toBeTruthy();
      }
    });
  });

  test.concurrent('hvigor-config.json5 √ cli ×', async () => {
    await initByStageEmptyApp({
      cb: async (dir) => {
        const appHvigorfile = path.resolve(dir, 'hvigorfile.ts');
        fs.writeFileSync(appHvigorfile, 'import {startParameters} from \'@ohos/hvigor\';\n' +
          'console.log(startParameters);\n' +
          'export { appTasks } from \'@ohos/hvigor-ohos-plugin\';');
        const hvigorConfigJsonFile = path.resolve(dir, 'hvigor/hvigor-config.json5');
        fse.ensureFileSync(hvigorConfigJsonFile);
        fs.writeFileSync(hvigorConfigJsonFile, '{\n' +
          '  "hvigorVersion": "2.0.0",\n' +
          '  "execution": {\n' +
          '    "daemon": false,\n' +
          '    "incremental": false,\n' +
          '    "parallel": false,\n' +
          '    "typeCheck": true\n' +
          '  },\n' +
          '  "logging": {\n' +
          '    "level": \'debug\'\n' +
          '  },\n' +
          '  "debugging": {\n' +
          '    "stacktrace": true\n' +
          '  }\n' +
          '}');
        const commands = initExecuteCommand({
          mode: 'module',
          product: 'default',
          module: 'entry@default',
          tasks: 'default@PreBuild'
        });
        const result = runHvigor(dir, commands, { stderr: 'pipe' });
        expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
        expect(result.stdout.includes('hvigorfileTypeCheck: true')).toBeTruthy();
        expect(result.stdout.includes('parallelExecution: false')).toBeTruthy();
        expect(result.stdout.includes('incrementalExecution: false')).toBeTruthy();
        expect(result.stdout.includes('printStackTrace: true')).toBeTruthy();
        expect(result.stdout.includes('daemon: false')).toBeTruthy();
        expect(result.stdout.includes('levelStr: \'DEBUG\'')).toBeTruthy();
      }
    });
  });

  test.concurrent('hvigor-config.json5 x cli √', async () => {
    await initByStageEmptyApp({
      cb: async (dir) => {
        const appHvigorfile = path.resolve(dir, 'hvigorfile.ts');
        fs.writeFileSync(appHvigorfile, 'import {startParameters} from \'@ohos/hvigor\';\n' +
          'console.log(startParameters);\n' +
          'export { appTasks } from \'@ohos/hvigor-ohos-plugin\';');
        const commands = initExecuteCommand({
          mode: 'module',
          product: 'default',
          module: 'entry@default',
          tasks: 'default@PreBuild',
        });
        commands.push('--no-daemon');
        commands.push('--no-incremental');
        commands.push('--no-parallel');
        commands.push('--type-check');
        commands.push('--debug');
        commands.push('--stacktrace');
        const result = runHvigor(dir, commands, { stderr: 'pipe' });
        expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
        expect(result.stdout.includes('hvigorfileTypeCheck: true')).toBeTruthy();
        expect(result.stdout.includes('parallelExecution: false')).toBeTruthy();
        expect(result.stdout.includes('incrementalExecution: false')).toBeTruthy();
        expect(result.stdout.includes('printStackTrace: true')).toBeTruthy();
        expect(result.stdout.includes('daemon: false')).toBeTruthy();
        expect(result.stdout.includes('levelStr: \'DEBUG\'')).toBeTruthy();
      }
    });
  });

  test.concurrent('hvigor-config.json5 √ cli √', async () => {
    await initByStageEmptyApp({
      cb: async (dir) => {
        const appHvigorfile = path.resolve(dir, 'hvigorfile.ts');
        fs.writeFileSync(appHvigorfile, 'import {startParameters} from \'@ohos/hvigor\';\n' +
          'console.log(startParameters);\n' +
          'export { appTasks } from \'@ohos/hvigor-ohos-plugin\';');
        const hvigorConfigJsonFile = path.resolve(dir, 'hvigor/hvigor-config.json5');
        fse.ensureFileSync(hvigorConfigJsonFile);
        fs.writeFileSync(hvigorConfigJsonFile, '{\n' +
          '  "hvigorVersion": "2.0.0",\n' +
          '  "execution": {\n' +
          '    "daemon": true,\n' +
          '    "incremental": true,\n' +
          '    "parallel": true,\n' +
          '    "typeCheck": false\n' +
          '  },\n' +
          '  "logging": {\n' +
          '    "level": \'info\'\n' +
          '  },\n' +
          '  "debugging": {\n' +
          '    "stacktrace": false\n' +
          '  }\n' +
          '}');
        const commands = initExecuteCommand({
          mode: 'module',
          product: 'default',
          module: 'entry@default',
          tasks: 'default@PreBuild',
        });
        commands.push('--no-daemon');
        commands.push('--no-incremental');
        commands.push('--no-parallel');
        commands.push('--type-check');
        commands.push('--debug');
        commands.push('--stacktrace');
        const result = runHvigor(dir, commands, { stderr: 'pipe' });
        expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
        expect(result.stdout.includes('hvigorfileTypeCheck: true')).toBeTruthy();
        expect(result.stdout.includes('parallelExecution: false')).toBeTruthy();
        expect(result.stdout.includes('incrementalExecution: false')).toBeTruthy();
        expect(result.stdout.includes('printStackTrace: true')).toBeTruthy();
        expect(result.stdout.includes('daemon: false')).toBeTruthy();
        expect(result.stdout.includes('levelStr: \'DEBUG\'')).toBeTruthy();
      }
    });
  });
});

describe('test hvigor-config.json5 startEnvironment', () => {
  test.concurrent('hvigor-config.json5 x cli ×', async () => {
    await initByStageEmptyApp({
      cb: async (dir) => {
        process.env.NODE_HOME = 'xxx';
        const appHvigorfile = path.resolve(dir, 'hvigorfile.ts');
        fs.writeFileSync(appHvigorfile, 'import {startEnvironment} from \'@ohos/hvigor\';\n' +
          'console.log(startEnvironment);\n' +
          'export { appTasks } from \'@ohos/hvigor-ohos-plugin\';');
        const commands = initExecuteCommand({
          mode: 'module',
          product: 'default',
          module: 'entry@default',
          tasks: 'default@PreBuild',
        });
        const result = runHvigor(dir, commands, { stderr: 'pipe' });
        expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
        expect(result.stdout.includes('nodeHome: \'xxx\'')).toBeTruthy();
        const hvigorUserHomeInfo = `hvigorUserHome: '${commonConfig.hvigorUserHome}'`.replace(/\\/g, '\\\\');
        expect(result.stdout.includes(hvigorUserHomeInfo)).toBeTruthy();
      }
    });
  });

  test.concurrent('hvigor-config.json5 √ cli ×', async () => {
    await initByStageEmptyApp({
      cb: async (dir) => {
        const appHvigorfile = path.resolve(dir, 'hvigorfile.ts');
        fs.writeFileSync(appHvigorfile, 'import {startEnvironment} from \'@ohos/hvigor\';\n' +
          'console.log(startEnvironment);\n' +
          'export { appTasks } from \'@ohos/hvigor-ohos-plugin\';');
        const hvigorConfigJsonFile = path.resolve(dir, 'hvigor/hvigor-config.json5');
        fse.ensureFileSync(hvigorConfigJsonFile);
        fs.writeFileSync(hvigorConfigJsonFile, '{\n' +
          '  "hvigorVersion": "2.0.0",\n' +
          '  "environment": {\n' +
          '    "nodeHome": "path/to/nodejs.exe",\n' +
          '    "hvigorUserHome": "path/to/hvigor/user/home"\n' +
          '  }\n' +
          '}');
        const commands = initExecuteCommand({
          mode: 'module',
          product: 'default',
          module: 'entry@default',
          tasks: 'default@PreBuild',
        });
        const result = runHvigor(dir, commands, { stderr: 'pipe' });
        expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
        expect(result.stdout.includes('nodeHome: \'path/to/nodejs.exe\'')).toBeTruthy();
        const hvigorUserHomeInfo = 'hvigorUserHome: \'path/to/hvigor/user/home\'';
        expect(result.stdout.includes(hvigorUserHomeInfo)).toBeTruthy();
      }
    });
  });

  test.concurrent('hvigor-config.json5 x cli √', async () => {
    await initByStageEmptyApp({
      cb: async (dir) => {
        const appHvigorfile = path.resolve(dir, 'hvigorfile.ts');
        fs.writeFileSync(appHvigorfile, 'import {startEnvironment} from \'@ohos/hvigor\';\n' +
          'console.log(startEnvironment);\n' +
          'export { appTasks } from \'@ohos/hvigor-ohos-plugin\';');
        const commands = initExecuteCommand({
          mode: 'module',
          product: 'default',
          module: 'entry@default',
          tasks: 'default@PreBuild',
        });
        const result = runHvigor(dir, commands, { stderr: 'pipe' });
        expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();

        // 暂时注释 待需求合入后重启测试
        // expect(result.stdout.includes('nodeHome: \'path/to/nodejs.exe\'')).toBeTruthy();
        // const hvigorUserHomeInfo = 'hvigorUserHome: \'path/to/hvigor/user/home\'';
        // expect(result.stdout.includes(hvigorUserHomeInfo)).toBeTruthy();
      }
    });
  });

  test.concurrent('hvigor-config.json5 √ cli √', async () => {
    await initByStageEmptyApp({
      cb: async (dir) => {
        const appHvigorfile = path.resolve(dir, 'hvigorfile.ts');
        fs.writeFileSync(appHvigorfile, 'import {startEnvironment} from \'@ohos/hvigor\';\n' +
          'console.log(startEnvironment);\n' +
          'export { appTasks } from \'@ohos/hvigor-ohos-plugin\';');
        const hvigorConfigJsonFile = path.resolve(dir, 'hvigor/hvigor-config.json5');
        fse.ensureFileSync(hvigorConfigJsonFile);
        fs.writeFileSync(hvigorConfigJsonFile, '{\n' +
          '  "hvigorVersion": "2.0.0",\n' +
          '  "environment": {\n' +
          '    "nodeHome": "1path/to/nodejs.exe",\n' +
          '    "hvigorUserHome": "1path/to/hvigor/user/home"\n' +
          '  }\n' +
          '}');
        const commands = initExecuteCommand({
          mode: 'module',
          product: 'default',
          module: 'entry@default',
          tasks: 'default@PreBuild',
        });
        const result = runHvigor(dir, commands, { stderr: 'pipe' });
        expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();

        // 暂时注释 待需求合入后重启测试
        // expect(result.stdout.includes('nodeHome: \'2path/to/nodejs.exe\'')).toBeTruthy();
        // const hvigorUserHomeInfo = 'hvigorUserHome: \'2path/to/hvigor/user/home\'';
        // expect(result.stdout.includes(hvigorUserHomeInfo)).toBeTruthy();
      }
    });
  });
});
